package com.sina.interfaces.manage.web.controller.user;

import com.sina.interfaces.core.exception.server.UpdateException;
import com.sina.interfaces.core.web.BaseController;
import com.sina.interfaces.domain.user.User;
import com.sina.interfaces.manage.service.user.UserManageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2017-11-14-17:25
 */
@Controller
@RequestMapping("userManage")
public class UserManageController extends BaseController {

    @Autowired
    @Qualifier("userManageService")
    private UserManageService userManageService;

    @RequestMapping(value = "getUserList", method = RequestMethod.GET)
    public ModelAndView getUserList(@Validated User user) {
        LOG.info("user:{}", user);

        ModelAndView mv = new ModelAndView(DEFAULT_VIEW);
        Long[] count = new Long[1];
        List<Map<String, Object>> list = userManageService.getUserList(user, count);

        Map<String, Object> result = new HashMap<>(4);
        result.put(DATA, list);
        result.put(RECORDS_FILTERED, count[0]);

        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "getUser", method = RequestMethod.GET)
    public ModelAndView getUser(String name) {
        LOG.info("name:{}", name);

        ModelAndView mv = new ModelAndView("resultView");
        User user = userManageService.getUser(name);
        mv.addObject(RESULT, user);

        return mv;
    }

    @RequestMapping(value = "deleteUser", method = RequestMethod.GET)
    public ModelAndView deleteUser(String id) {
        LOG.info("id:{}", id);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);

        try {
            userManageService.delete(Long.valueOf(id));
            result.put(CODE, OK);
            result.put(MSG, "删除用户成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "updateUser", method = RequestMethod.GET)
    public ModelAndView updateUser(@Valid User user) {
        LOG.info("user:{}", user);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            userManageService.updateUser(user);
            result.put(CODE, OK);
            result.put(MSG, "更新用户成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "saveUser", method = RequestMethod.GET)
    public ModelAndView saveUser(@Valid User user) {
        LOG.info("user:{}", user);

        ModelAndView mv = new ModelAndView("resultView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            userManageService.saveUser(user);
            result.put(CODE, OK);
            result.put(MSG, "新增用户成功");
        } catch (UpdateException e) {
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "getUserListView", method = RequestMethod.GET)
    public ModelAndView getUserListView() {
        ModelAndView mv = new ModelAndView("user/userList");
        return mv;
    }
}
